package foodmaterial.action;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import foodmaterial.config.ChoiceDataSource;
import foodmaterial.dto.*;
import foodmaterial.entity.*;
import foodmaterial.excel.Excel;
import foodmaterial.service.FoodMaterialService;
import foodmaterial.service.InitService;
import foodmaterial.service.StockService;
import foodmaterial.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

@RequestMapping("/api/stock")
@RestController
@CrossOrigin(allowCredentials = "true")
public class StockAction extends BaseAction {

    @Autowired
    private StockService service ;

    @Autowired
    private InitService initService ;

    @Autowired
    private FoodMaterialService foodMaterialService ;

    @Autowired
    private Excel excel;

    @PostMapping("/addStockBill")
    @ChoiceDataSource
    public JsonResult<String> addStockBill(@RequestBody @Valid StockBill stockDetail, BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        int result = service.addStockBill(stockDetail) ;
        return result > 0 ? JsonResultTool.get(jsonResult,true, "新增损溢单成功") :
                JsonResultTool.get(jsonResult,false,"新增损溢单失败") ;
    }

    @PostMapping("/modifyStockBill")
    @ChoiceDataSource
    public JsonResult<String> modifyStockBill(@RequestBody @Valid StockBill stockDetail, BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        int result = service.modifyStockBill(stockDetail) ;
        return result > 0 ? JsonResultTool.get(jsonResult,true, "修改损溢单成功") :
                JsonResultTool.get(jsonResult,false,"修改损溢单失败") ;
    }

    @PostMapping("/queryStockBill")
    @ChoiceDataSource
    public JsonResult<PageInfo<StockBill>> queryStockBill(@RequestBody StockBillDto dto) {
        PageHelper.startPage(dto.getPageNo(),dto.getPageSize()) ;
        List<StockBill> result = service.queryStockBill(dto) ;
        PageInfo<StockBill> pageInfo = new PageInfo<>(result) ;
        return new JsonResult(true, ResultCode.SUCCESS,pageInfo) ;
    }

    @GetMapping("/queryModifyBill")
    @ChoiceDataSource
    public JsonResult<StockBill> queryModifyBill(@RequestParam("billId") Long billId) {
        StockBill result = service.queryModifyBill(billId) ;
        return new JsonResult(true, ResultCode.SUCCESS,result) ;
    }

    @GetMapping("/queryStockBillDetail")
    @ChoiceDataSource
    public JsonResult<List<StockBillDetail>> queryStockBillDetail(Long billId) {
        List<StockBillDetail> result = service.queryStockBillDetail(billId) ;
        return new JsonResult(true, ResultCode.SUCCESS,result) ;
    }

    @GetMapping("/printStockBill")
    public ModelAndView printStockBill(@RequestParam("billIds") List<Long> billIds) {
        StockBillDto dto = new StockBillDto() ;
        dto.setBillIds(billIds);
        dto.setBackAddress(initService.querySysParamsByCode(Constants.BACK_SERVER_ADDRESS).getParamValue());
        return new ModelAndView("printStockBill","params",dto);
    }

    @GetMapping("/printProcessBill")
    public ModelAndView printProcessBill(@RequestParam("billIds") List<Long> billIds) {
        ProcessBillDto dto = new ProcessBillDto() ;
        dto.setBillIds(billIds);
        dto.setBackAddress(initService.querySysParamsByCode(Constants.BACK_SERVER_ADDRESS).getParamValue());
        return new ModelAndView("printProcessBill","params",dto);
    }

    @PostMapping("/printStockBillData")
    @ChoiceDataSource
    public JsonResult<List<StockBill>> printStockBillData(@RequestBody StockBillDto dto) {
        List<StockBill> result = service.queryPrintStockBill(dto.getBillIds()) ;
        return new JsonResult(true, ResultCode.SUCCESS,result) ;
    }

    @PostMapping("/printProcessBillData")
    @ChoiceDataSource
    public JsonResult<List<ProcessBill>> printProcessBillData(@RequestBody ProcessBillDto dto) {
        List<ProcessBill> result = service.queryPrintProcessBill(dto.getBillIds()) ;
        return new JsonResult(true, ResultCode.SUCCESS,result) ;
    }

    @GetMapping("/printStock")
    public ModelAndView printStock(FoodMaterialDto dto, HttpServletRequest request) {
        dto.setBackAddress(initService.querySysParamsByCode(Constants.BACK_SERVER_ADDRESS).getParamValue());
        if (request.getParameter("originalProcess") != null)
            dto.setOriginalProcess(Boolean.valueOf(request.getParameter("originalProcess")));
        return new ModelAndView("printStock","params",dto);
    }

    @PostMapping("/printStockData")
    @ChoiceDataSource
    public JsonResult<List<FoodMaterial>> printStockData(@RequestBody FoodMaterialDto dto) {
        dto.setStop(false);
        List<FoodMaterial> result = foodMaterialService.queryFoodMaterial(dto) ;
        return new JsonResult(true, ResultCode.SUCCESS,result) ;
    }

    @PostMapping("/addProductionLine")
    @ChoiceDataSource
    public JsonResult<String> addProductionLine(@RequestBody @Valid ProductionLine productionLine,
                                                  BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        String result = service.addProductionLine(productionLine) ;
        return StringUtils.isEmpty(result) ? JsonResultTool.get(jsonResult,true, "新增生产线成功") :
                JsonResultTool.get(jsonResult,false,result) ;
    }

    @PostMapping("/modifyProductionLine")
    @ChoiceDataSource
    public JsonResult<String> modifyProductionLine(@RequestBody @Valid ProductionLine productionLine,
                                                     BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        String result = service.modifyProductionLine(productionLine) ;
        return StringUtils.isEmpty(result) ? JsonResultTool.get(jsonResult,true, "修改生产线成功") :
                JsonResultTool.get(jsonResult,false,result) ;
    }

    @PostMapping("/queryProductionLine")
    @ChoiceDataSource
    public JsonResult<PageInfo<ProductionLine>> queryProductionLine(@RequestBody ProductionLineDto dto) {
        PageHelper.startPage(dto.getPageNo(),dto.getPageSize()) ;
        List<ProductionLine> result = service.queryProductionLine(dto) ;
        PageInfo<ProductionLine> pageInfo = new PageInfo<>(result) ;
        return new JsonResult(true, ResultCode.SUCCESS,pageInfo) ;
    }

    @PostMapping("/addProcessBill")
    @ChoiceDataSource
    public JsonResult<String> addProcessBill(@RequestBody @Valid ProcessBill processBill,
                                                BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        int result = service.addProcessBill(processBill) ;
        return result > 0 ? JsonResultTool.get(jsonResult,true, "新增净菜加工单成功") :
                JsonResultTool.get(jsonResult,false,"新增净菜加工单失败") ;
    }

    @PostMapping("/modifyProcessBill")
    @ChoiceDataSource
    public JsonResult<String> modifyProcessBill(@RequestBody @Valid ProcessBill processBill,
                                             BindingResult bindingResult) {
        JsonResult jsonResult = BindingResultUtil.error(bindingResult) ;
        if (jsonResult.getSuccess() == false) return jsonResult ;
        int result = service.modifyProcessBill(processBill) ;
        return result > 0 ? JsonResultTool.get(jsonResult,true, "修改净菜加工单成功") :
                JsonResultTool.get(jsonResult,false,"修改净菜加工单失败") ;
    }

    @PostMapping("/queryProcessBill")
    @ChoiceDataSource
    public JsonResult<PageInfo<ProcessBill>> queryProcessBill(@RequestBody ProcessBillDto dto) {
        PageHelper.startPage(dto.getPageNo(),dto.getPageSize()) ;
        List<ProcessBill> result = service.queryProcessBill(dto) ;
        PageInfo<ProcessBill> pageInfo = new PageInfo<>(result) ;
        return new JsonResult(true, ResultCode.SUCCESS,pageInfo) ;
    }

    @GetMapping("/queryModifyProcessBill")
    @ChoiceDataSource
    public JsonResult<ProcessBill> queryModifyProcessBill(Long processBillId) {
        ProcessBill result = service.queryModifyProcessBill(processBillId) ;
        return new JsonResult(true, ResultCode.SUCCESS,result) ;
    }

    @GetMapping("/queryProcessBillDetail")
    @ChoiceDataSource
    public JsonResult<List<ProcessBillDetail>> queryProcessBillDetail(Long processBillId) {
        List<ProcessBillDetail> result = service.queryProcessBillDetail(processBillId) ;
        return new JsonResult(true, ResultCode.SUCCESS,result) ;
    }

    @PostMapping("/queryTotalProcess")
    @ChoiceDataSource
    public JsonResult<PageInfo<ProcessBillDetail>> queryTotalProcess(@RequestBody ProcessBillDto dto) {
        PageHelper.startPage(dto.getPageNo(),dto.getPageSize()) ;
        List<ProcessBillDetail> result = service.queryTotalProcess(dto) ;
        PageInfo<ProcessBillDetail> pageInfo = new PageInfo<>(result) ;
        return new JsonResult(true, ResultCode.SUCCESS,pageInfo) ;
    }

    @GetMapping("/downloadProductionCapacity")
    @ChoiceDataSource
    public void downloadProductionCapacity(ProcessBillDto dto,
                             HttpServletResponse response) throws IOException {
        setDownload(response,excel);
        excel.downloadProductionCapacityExcel(dto,
                Constants.EXCEL_PRODUCTION_CAPACITY_TEMPLATE,response.getOutputStream());
    }

    @PostMapping("/queryProcessPlan")
    @ChoiceDataSource
    public JsonResult<PageInfo<ProcessBillDetail>> queryProcessPlan(@RequestBody PurchasePlanDto dto) {
        PageHelper.startPage(dto.getPageNo(),dto.getPageSize()) ;
        List<ProcessBillDetail> result = service.queryProcessPlan(dto) ;
        PageInfo<ProcessBillDetail> pageInfo = new PageInfo<>(result) ;
        return new JsonResult(true, ResultCode.SUCCESS,pageInfo) ;
    }

    @GetMapping("/downloadProcessPlan")
    @ChoiceDataSource
    public void downloadProcessPlan(PurchasePlanDto dto, HttpServletResponse response) throws IOException {
        setDownload(response,excel);
        dto.setKitchen(Constants.KITCHEN_FOOD.equals(this.getSysUser().getFoodSupplyType()));
        excel.downloadProcessPlanExcel(dto,
                Constants.EXCEL_PROCESS_PLAN_TEMPLATE,response.getOutputStream());
    }

}
